<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/home' }">用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加用户按钮 -->
    <el-card class="box-card">
      <el-input
        placeholder="请输入内容"
        v-model="queryinfo.query"
        clearable
        class="serch"
      ></el-input>
      <el-button icon="el-icon-search" border="0" @click="getUsers"
        >搜索</el-button
      >
      <el-button type="primary" @click="dialogFormVisible = true"
        >添加用户</el-button
      >
    </el-card>
    <!-- 表头区域 -->
    <el-table :data="userList" border style="width: 100%">
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="role_name" label="角色"> </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <!-- 修改用户状态 -->
      <el-table-column prop="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch
            active-color="#13ce66"
            inactive-color="#ff4949"
            v-model="scope.row.mg_state"
            @change="changeState(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <!-- 修改用户操作 -->
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <!-- 修改用户信息 -->
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="showEditUser(scope.row)"
          ></el-button>
          <!-- 改变用户角色 -->
          <el-button
            type="info"
            size="mini"
            icon="el-icon-message"
            circle
            @click="changeRoles(scope.row)"
          ></el-button>
          <!-- 删除用户 -->
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="deleteuser(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryinfo.pagenum"
      :page-sizes="[3, 5, 7, 10]"
      :page-size="queryinfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加用户弹窗 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="addusers">
        <el-form-item label="用户名">
          <el-input v-model="addusers.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="addusers.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="addusers.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="addusers.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUsers">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改用户信息弹窗 -->
    <el-dialog title="修改用户" :visible.sync="dialogEditUsser">
      <el-form :model="putEditUser">
        <el-form-item label="用户名">
          <el-input
            v-model="putEditUser.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="putEditUser.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="putEditUser.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="outputEdit">取 消</el-button>
        <el-button type="primary" @click="putEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改用户角色 -->
    <el-dialog title="修改角色" :visible.sync="dialogUsserRoles">
      <el-form :model="userRolesData">
        <el-form-item label="用户名">
          <el-input
            v-model="userRolesData.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-select v-model="roleId" placeholder="请选择">
          <el-option
            v-for="item in roleList"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="outputRoles">取 消</el-button>
        <el-button type="primary" @click="putUserRoles">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //遍历数组
      userList: [],
      //统计条数
      total: 0,
      queryinfo: {
        //查询参数
        querry: "",
        //显示条数
        pagesize: 3,
        //页数
        pagenum: 1,
      },
      //添加用户数据
      addusers: {
        username: "",
        pssword: "",
        email: "",
        mobile: "",
      },
      //添加用户弹窗
      dialogFormVisible: false,
      //修改用户信息弹窗
      dialogEditUsser: false,
      //修改用户数据对象
      putEditUser: {},
      //修改用户角色
      dialogUsserRoles: false,
      //用户角色对象遍历
      userRolesData: {},
      //遍历下拉菜单的角色名
      roleList: [],
      //数据绑定
      roleId: "",
    };
  },
  methods: {
    //获取用户的数据
    //封装getUsers（——）
    async getUsers() {
      //获取用户数据
      var { data } = await this.$axios.get(`/users`, {
        params: this.queryinfo,
      });
      if (data.meta.status == 200) {
        //渲染页面的数组为都断获取的
        this.userList = data.data.users;
        //总数
        this.total = data.data.total;
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    // pageSize 改变时会触发
    handleSizeChange(newsize) {
      this.queryinfo.pagesize = newsize;
      this.getUsers();
    },
    //currentPage 改变时会触发
    handleCurrentChange(newpage) {
      this.queryinfo.pagenum = newpage;
      this.getUsers();
    },
    //添加用户信息
    async addUsers() {
      var { data } = await this.$axios.post("/users", this.addusers);
      // console.log(data);
      //创建成功提示
      if (data.meta.status == 201) {
        this.$message({
          type: "success",
          message: data.meta.msg,
        });
        //弹窗消失
        this.dialogFormVisible = false;
        this.getUsers();
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    // 修改用户状态
    async changeState(rows) {
      var { data } = await this.$axios.put(
        `/users/${rows.id}/state/${rows.mg_state}`
      );
      // console.log(data);
      //提示
      if (data.meta.status == 200) {
        this.$message({
          type: "success",
          message: data.meta.msg,
        });
      } else {
        //当修改状态发生错误时，页面显示的内容不变
        this.mg_state = !this.mg_state;
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    //修改用户信息提前渲染进弹窗
    showEditUser(userinfo) {
      this.putEditUser = userinfo;
      //显示弹窗
      this.dialogEditUsser = true;
    },
    //修改用户信息
    async putEdit() {
      //发送请求
      var { data } = await this.$axios.put(
        `users/` + this.putEditUser.id,
        this.putEditUser
      );
      // console.log(data);
      //弹窗关闭
      this.dialogEditUsser = false;
      //弹出提醒
      if (data.meta.status == 200) {
        this.$message({
          type: "success",
          message: data.meta.msg,
        });
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    //取消用户修改
    outputEdit() {
      this.dialogEditUsser = false;
      //调用渲染数据函数
      this.getUsers();
    },
    //删除用户
    deleteuser(userinfo) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          var { data } = await this.$axios.delete("/users/" + userinfo.id);
          console.log(data);
          if (data.meta.status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //删除成功刷新页面
            this.getUsers();
          } else {
            this.$message({
              type: "error",
              message: data.meta.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改用户角色按钮
    async changeRoles(userinfo) {
      // 显示弹窗
      this.dialogUsserRoles = true;
      // 用户数据传输
      this.userRolesData = userinfo;
      //获取所有角色信息
      var { data } = await this.$axios.get("roles");
      // console.log(data);
      this.roleList = data.data;
    },
    //修改用户角色
    async putUserRoles() {
      var { data } = await this.$axios.put(
        `users/${this.userRolesData.id}/role`,
        { rid: this.roleId }
      );
      console.log(data);
      this.dialogUsserRoles = false;
      if (data.meta.status == 200) {
        this.$message({
          type: "success",
          message: data.meta.msg,
        });
        //重新渲染
        this.getUsers();
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    //取消修改用户角色
    outputRoles() {
      // 关闭弹窗
      this.dialogUsserRoles = false;
    },
  },
  async mounted() {
    //用户数据渲染调用
    this.getUsers();
  },
};
</script>

<style>
.el-table-column {
  height: 50px;
}
.has-gutter {
  line-height: 35px;
}
.el-row {
  line-height: 60px;
}
.el-dialog__header {
  line-height: 0px;
}
.dialog-footer {
  line-height: 0px;
}
.el-dialog {
  width: 25vw;
}
.el-input {
  line-height: 25px;
}
.el-form-item__content {
  width: 23vw;
}
.el-dialog__header {
  line-height: 0px;
}
.dialog-footer {
  line-height: 0px;
}
.has-gutter {
  line-height: 35px;
}
.el-row {
  line-height: 60px;
}
.el-card__body {
  line-height: 0px;
}
.serch {
  width: 22vw !important;
}
</style>